import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import scipy.interpolate as spi

plt.rcParams["font.sans-serif"] = ["SimHei"]
plt.rcParams["axes.unicode_minus"] = False
data1 = pd.read_csv("data1.csv")

# data1.plot()
# plt.show()
# quit()

X = data1.index
Y = data1.values
x = np.arange(0, len(data1), 0.15)

ipo1 = spi.splrep(X, Y, k=1)
ipo3 = spi.splrep(X, Y, k=3)
iy1 = spi.splev(x, ipo1)
# 插值有點擬合的意思，就是讓數據更加光滑，不會那麽多的摺叠啊，這種三次插值法應該要確定
iy3 = spi.splev(x, ipo3)
fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(10, 12))
ax1.plot(X, Y, label='沪深300')
ax1.plot(x, iy1, 'r.', label='插值点')
ax1.set_ylim(Y.min() - 10, Y.max() + 10)
ax1.set_ylabel('指数')
ax1.set_title('线性插值')
ax1.legend()
ax2.plot(X, Y, label='沪深300')
ax2.plot(x, iy3, 'r.', label='插值点')
ax2.set_ylim(Y.min() - 10, Y.max() + 10)
ax2.set_ylabel('指数')
ax2.set_title('三次样条插值')
ax2.legend()
plt.show()